package com.zjl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjl.common.R;
import com.zjl.dto.SetmealDto;
import com.zjl.entity.Setmeal;
import com.zjl.service.SetmealDishService;
import com.zjl.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping(value = "/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐信息，同时新增套餐对应的SetmealDish数据
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 套餐分页信息查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(value = "page")
    public R<Page<Setmeal>> page(@RequestParam(value = "name",required = false) String name,
                                 @RequestParam(value = "page") Integer page,
                                 @RequestParam(value = "pageSize") Integer pageSize)
    {
        Page<Setmeal> setmealPage = setmealService.getPage(page, pageSize, name);
        return R.success(setmealPage);
    }

    /**
     * 删除套餐信息，并删除SetmealDish表中于当前套餐关联的数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> removeWithSetmealDish(@RequestParam List<Long> ids){
        setmealService.removeWithSetmealDish(ids);
        return R.success("成功删除");
    }

    //根据条件查询套餐
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        return R.success(setmealService.getByConditions(setmeal));
    }


}
